package com.zty.xiaomiadmin.server.Controller;

import com.zty.xiaomiadmin.server.Entity.Result;
import com.zty.xiaomiadmin.server.Service.User.UserServiceImp;
import com.zty.xiaomiadmin.server.Vo.User.UserResult;
import com.zty.xiaomiadmin.server.utils.QiniuUtils;
import com.zty.xiaomiadmin.server.utils.UuidUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

@CrossOrigin
@RestController
@RequestMapping("upload")
public class UploadController {
    //上传图片到七牛云
    @RequestMapping("/uploadImg")
    public Result uploadCoverImg(MultipartFile file) {
        String originalFilename = file.getOriginalFilename();
        int index = originalFilename.lastIndexOf(".");
        String extention = originalFilename.substring(index - 1);
        String fileName = UuidUtils.getUuid() + extention;
        try {
            QiniuUtils.upload2Qiniu(file.getBytes(), fileName);
        } catch (IOException e) {
            e.printStackTrace();
            return new Result(500, "上传失败",null);
        }
        return new Result(200, "上传图片成功", QiniuUtils.qiniuURL + fileName);
    }
}
